<?php
$this->codextemplates->js('js-tablesorter','jquery.tablesorter.pack.js');

$table_sorter_js = "

    $(document).ready(function()
        {
            $('#codex-table').tablesorter({widgets:['zebra']});
        }
    );

";

$this->codextemplates->inlineJS('js-tablesorter-init', $table_sorter_js);

function message_output($type,$message){
    $output = '';

    switch($type){
        case 'success':
            $prefix = '<div class="success">';
            $suffix = '</div>';
        break;
        case 'info':
            $prefix = '<div class="info">';
            $suffix = '</div>';
        break;
        case 'failure':
            $prefix = '<div class="failure">';
            $suffix = '</div>';
        break;
        default:
            $prefix = '<div class="info">';
            $suffix = '</div>';
        break;
    }
    if(is_array($message)){
        foreach($message as $m){
            $output .= $prefix.$m.$suffix."\n";
        }
    }
    else
        $output .= $prefix.$message.$suffix."\n";
    return $output;
}
?>
<div class="codex-table">
    <div class="column span-50 first">
        <div id="sub-header-left">
            <?php echo $this->codexcrumbs->getAsHTML(); ?>
            <div id="add-new">
                <?php echo '<a class="addtrigger" href="'.site_url().'/'.$add_link.'"><img src="'.$this->config->item('codex_asset_folder').'/images/add.gif"></a>'; ?>
            </div>
        </div>
        <div class="clear"></div>
        <?php echo $this->pagination->create_links(); ?>
        <div id="messages"> 

            <?php
                if(count($messages) > 0){
                    foreach($messages as $type=>$message){
                        echo message_output($type,$message);
                    }
                }

            ?> 

            <?php if(count($entries) == 0) echo '<div class="failure">'.$this->lang->line('codexadmin_no_entries').'</div>'; else {?>

        </div>
        <?php echo form_open($this->delete_action,array('class'=>'manager-form')); ?>
		<table id="codex-table">
            <thead>
			<tr class="header-row">
				<?php 
					$count = count($this->display_fields);
                    echo '<th colspan="1"></th>';
					for($i=0;$i<$count;$i++){
                                if(function_exists('humanize'))
                                    echo '<th>', humanize($this->display_fields[$i]), '</th>';
                                else
                                    echo '<th>', ucfirst($this->display_fields[$i]), '</th>';
                          } 
                ?>
			</tr>
            </thead>
            <tbody>
        <?php $j=0;foreach($entries as $entry): 
            if($j % 2 == 0)                 
			    echo '<tr class="even">';
            else
			    echo '<tr class="odd">';
?>
				<td class="first"><input class="edit-button" type="checkbox" value="<?php echo $entry[$this->codexadmin->primary_key]?>" name="selected_rows[]"/></td>
                   <?php for($i=0;$i<$count;$i++){
                           echo '<td>';
                           if(function_exists('humanize'))
                               if($i==0){
                                    $anchor = $entry[$this->display_fields[$i]];

                                    if(empty($anchor))
                                        $anchor = 'Click to edit';
                                    else if(strlen($anchor) > 30){
                                        $anchor = substr($anchor,15).'...'.substr($anchor,-15);
                                    }

                                    echo anchor(str_replace('{num}',$entry[$this->codexadmin->primary_key],$this->edit_link),$anchor).'</td>';
                               }
                               else
                                   echo $entry[$this->display_fields[$i]],'</td>';
                          } 
                    ?>

            </tr>
        <?php $j++; endforeach; ?>
            </tbody>
		</table>
        <?php echo $this->pagination->create_links(); ?>

        <input type="submit" value="<?php echo $this->lang->line('codex_delete_selected'); ?>" name="delete_selected" id="delete-selected">
        <?php echo form_close(); ?></td>

        <?php } ?>
    </div>
</div>
